package _03TCP通信;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class TCPClient {
    public static void main(String[] args) throws IOException {
        //1.创建Socket对象，与指定服务端连接
        Socket socket = new Socket("127.0.0.1",8888);
        //2.通过socket得到输出流写数据到服务器
        OutputStream os = socket.getOutputStream();
        os.write("你好，服务端".getBytes());

        //3.通过socket得到输入流获取服务器回写的数据
        InputStream is = socket.getInputStream();

        //4.读取数据
        byte[] bytes = new byte[1024];
        int read = is.read(bytes);
        System.out.println("服务端的回复"+new String(bytes,0,read));

        is.close();
        os.close();
        socket.close();
    }
}
